package com.xiehua.task.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.xiehua.task.dto.Job;
import com.xiehua.task.job.pool.Job.JobPage;
import com.xiehua.task.service.impl.ListServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiehua.task.domain.ListDO;
import com.xiehua.task.service.ListService;
import com.xiehua.common.utils.PageUtils;
import com.xiehua.common.utils.Query;
import com.xiehua.common.utils.R;
import org.tio.utils.json.Json;

/**
 * 任务客户端配置信息表
 * 
 * @author Mr.zhang
 * @email 562464314@qq.com
 * @date 2018-07-11 10:25:53
 */
 
@Controller
@RequestMapping("/task/list")
public class ListController {
	@Autowired
	private ListService listService;
	
	@GetMapping()
	@RequiresPermissions("task:list:list")
	String List(){
	    return "task/list/list";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("task:list:list")
	public PageUtils list(@RequestParam Map<String, Object> params)throws Exception{
		//查询列表数据
        Query query = new Query(params);
		JobPage page = listService.list(query);
		PageUtils pageUtils = new PageUtils(page.getList().stream().map(s -> {
		    Job t = new Job();
		    BeanUtils.copyProperties(s,t);
		    t.setCreateTime(s.getCreateTime().format(ListServiceImpl.formatter));
			t.setUpdateTime(s.getUpdateTime().format(ListServiceImpl.formatter));
		    return t;
        }).collect(Collectors.toList()), page.getTotalRows());
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("task:list:add")
	String add(){
	    return "task/list/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("task:list:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		ListDO list = listService.get(id);
		model.addAttribute("list", list);
	    return "task/list/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("task:list:add")
	public R save( ListDO list){
		if(listService.save(list)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("task:list:edit")
	public R update( ListDO list){
		listService.update(list);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("task:list:remove")
	public R remove( Integer id){
		if(listService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("task:list:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		listService.batchRemove(ids);
		return R.ok();
	}
	
}
